package cn.cdzdjd.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import cn.cdzdjd.constant.BrowserConstant;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Response;

public class JsoupUtil {

	public static String downloadVideoUrl(String url) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true).followRedirects(false)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.execute();
			if (response.hasHeader("Location")) {
				return response.header("Location");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return url;
	}

	public static Response getUrlResponse(String url) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true).followRedirects(false)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.execute();
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static Document getDocument(String url) {
		try {
			Document doc = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.get();
			return doc;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Document getDocumentCookie(String url, Map<String, String> cookies) {
		try {
			Document doc = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).cookies(cookies)
					.referrer(url).get();
			return doc;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Response getResponseCookie(String url, Map<String, String> cookies) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).cookies(cookies)
					.referrer(url).execute();
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 允许跳转
	 * @param url
	 * @return
	 */
	public static Response getFinalUrlResponse(String url) {
		try {
			Response response = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true)
					.userAgent(BrowserConstant.USER_AGENT_PC).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.execute();
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static Document getDocumentMobile(String url) {
		try {
			Document doc = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true)
					.userAgent(BrowserConstant.USER_AGENT_MOBILE).header("Accept", BrowserConstant.ACCEPT).referrer(url)
					.get();
			return doc;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
